﻿using Halcon二次开发.Controller.OCR字符识别;
using Halcon二次开发.Controller.卡尺;
using Halcon二次开发.Controller.多模板匹配;
using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Model;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Model.自定义控件;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace Halcon二次开发.View.OCR字符识别
{
    public partial class OCRForm : Form
    {
        /// <summary>
        /// 每new一次就是相当于一个界面
        /// </summary>
        public OCRFormController controller;
        /// <summary>
        /// 将自定义界面进行存储
        /// </summary>
        public List<UserControl> listNodes = new List<UserControl>();
        /// <summary>
        /// 矩形参数
        /// </summary>
        public Rectangle2Control node1;
        /// <summary>
        /// 圆形参数
        /// </summary>
        public CirleControl node2;
        public OCRForm()
        {
            InitializeComponent();
            controller = new OCRFormController(this, $"..\\..\\Data\\工程文件\\{StaicResouce.HwindowFlowNodeUrse.name}工程文件\\OCR字符文件");
            toolStripComboBox1.SelectedIndex = 0;
            //矩形
            node1 = new Rectangle2Control();
            node1.Dock = DockStyle.Fill;
            node1.XUpDown.ValueChanged += Click;
            node1.YUpDown.ValueChanged += Click;
            node1.Length1UpDown.ValueChanged += Click;
            node1.Length2UpDown.ValueChanged += Click;
            node1.PhiUpDown.ValueChanged += Click;
            //圆形
            node2 = new CirleControl();
            node2.Dock = DockStyle.Fill;
            node2.XUpDown.ValueChanged += Click;
            node2.YUpDown.ValueChanged += Click;
            node2.RaduisUpDown.ValueChanged += Click;
            //将node进行存储
            listNodes.Add(node1);
            listNodes.Add(node2);
            //默认显示矩形
            groupBox3.Controls.Add(node1);
            //给HSmart绑定缩放事件
            hSmartWindowControl1.MouseWheel += HSmartWindowControl1_MouseWheel;
            hSmartWindowControl2.MouseWheel += HSmartWindowControl2_MouseWheel;
            hSmartWindowControl3.MouseWheel += HSmartWindowControl3_MouseWheel;
        }

        private void HSmartWindowControl3_MouseWheel(object sender, MouseEventArgs e)
        {
            hSmartWindowControl3.HSmartWindowControl_MouseWheel(sender, e);
        }

        private void HSmartWindowControl2_MouseWheel(object sender, MouseEventArgs e)
        {
            hSmartWindowControl2.HSmartWindowControl_MouseWheel(sender, e);
        }

        /// <summary>
        /// Halcon控件缩放开启
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HSmartWindowControl1_MouseWheel(object sender, MouseEventArgs e)
        {
            hSmartWindowControl1.HSmartWindowControl_MouseWheel(sender, e);
        }
        /// <summary>
        /// 反射调用的方法，请不要修改
        /// </summary>
        /// <param name="path">Json文件的路径</param>
        /// <param name="addres">节点</param>
        public void SetJsonPaths(string path, string addres)
        {
            string paths = path + "\\" + "界面数据\\" + addres + "\\" + addres + "数据.json";
            //调用存储方法
            new LoadData().LoadDatas(paths, this);
        }
        /// <summary>
        /// 模型移动数值更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Click(object sender, EventArgs e)
        {
            controller.Move(sender);
        }
        /// <summary>
        /// 防止被垃圾回收清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OCRForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
            controller.ClearTwoObject(StaicResouce.HwindowFlowNodeUrse.hSmartWindowControl1.HalconWindow);
        }
        /// <summary>
        /// 形状切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox3.Controls.Clear();
            string str = (sender as ToolStripComboBox).Text;
            if (str == "旋转矩形")
            {
                groupBox3.Controls.Add(node1);
            }
            else if (str == "圆形")
            {
                groupBox3.Controls.Add(node2);
            }
        }
        /// <summary>
        /// 开始画形状
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (controller.IntputImage == null || controller.ImageIsNg())
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + "当前图像为空!\r\n");
                return;
            }
            if (toolStripComboBox1.SelectedItem.ToString() == "旋转矩形")
            {
                controller.DrawRectangle2();

            }
            else if (toolStripComboBox1.SelectedItem.ToString() == "圆形")
            {
                controller.DrawCirle();
            }
        }
        /// <summary>
        /// 提取线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            controller.ExtractLine();
        }

        /// <summary>
        /// 放置区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            controller.SetRegion();
        }

        /// <summary>
        /// 工具的单次运行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            controller.Tool_Run();
        }
        /// <summary>
        /// 打开一个字符文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            controller.Open_File();
        }
        /// <summary>
        /// 字符区域鼠标点击区域事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hSmartWindowControl2_HMouseDown(object sender, HalconDotNet.HMouseEventArgs e)
        {
            controller.Mouse_MouseClick(sender, e);
        }

        /// <summary>
        /// 添加所有字符
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            controller.AddChars();
        }
        /// <summary>
        /// 添加单个字符
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            controller.AddChar();
        }
        /// <summary>
        /// 删除所有项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            controller.ClearChar();
        }
    }
}
